package practice抽奖箱;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class lotteryBox2 extends Thread{
   static ArrayList<Integer> list = new ArrayList<>();
   static ArrayList<Integer> list1 = new ArrayList<>();
   static ArrayList<Integer> list2 = new ArrayList<>();
    static{
        // 添加
        Collections.addAll(list,10,5,20,50,100,200,500,800,2,80,300,700);

        // 打乱
        Collections.shuffle(list);
    }
    static int count1 = 0;
    static int count2 = 0;
    static int sum1 = 0;
    static int sum2 = 0;
    static int max1 = 0;
    static int max2 = 0;

    @Override
    public void run() {
        while(true){
            synchronized (lotteryBox.class){
                if(list.size() == 0){
                    break;
                }else{
                    Integer remove = list.remove(0);
                    if(getName().equals("抽奖箱1")){
                        count1++;
                        sum1+=remove;
                        list1.add(remove);
                    }else{
                        count2++;
                        sum2+=remove;
                        list2.add(remove);
                    }
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        if(getName().equals("抽奖箱1")){
            System.out.println("在此次抽奖过程中,抽奖箱1总共产生了"+count1+"个奖项\r\n"+
                    "分别为:"+list1.toString()+"最高奖项为"+getMax(list1)+"元,总计额为"+sum1);
        }else{
            System.out.println("在此次抽奖过程中,抽奖箱2总共产生了"+count2+"个奖项\r\n"+
                    "分别为:"+list2.toString()+"最高奖项为"+getMax(list2)+"元,总计额为"+sum2);
        }
    }
    public int getMax(ArrayList<Integer> list){
        int max = 0;
        for (int i = 0; i < list.size(); i++) {
            int num = list.get(i);
            if(max<num){
                max = num;
            }
        }
        return max;
    }
}
